/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package negocio;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
import org.primefaces.model.chart.Axis;
import org.primefaces.model.chart.AxisType;
import org.primefaces.model.chart.CartesianChartModel;
import org.primefaces.model.chart.LineChartModel;
import org.primefaces.model.chart.LineChartSeries;
import org.primefaces.model.chart.MeterGaugeChartModel;

/**
 *
 * @author eriksigcha
 */
@Entity
@Table(name = "indicador")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Indicador.findAll", query = "SELECT i FROM Indicador i"),
    @NamedQuery(name = "Indicador.findByIdindicador", query = "SELECT i FROM Indicador i WHERE i.idindicador = :idindicador"),
    @NamedQuery(name = "Indicador.findByNombre", query = "SELECT i FROM Indicador i WHERE i.nombre = :nombre"),
    @NamedQuery(name = "Indicador.findByFormula", query = "SELECT i FROM Indicador i WHERE i.formula = :formula"),
    @NamedQuery(name = "Indicador.findByUnidad", query = "SELECT i FROM Indicador i WHERE i.unidad = :unidad"),
    @NamedQuery(name = "Indicador.findByBase", query = "SELECT i FROM Indicador i WHERE i.base = :base"),
    @NamedQuery(name = "Indicador.findByMedicion", query = "SELECT i FROM Indicador i WHERE i.medicion = :medicion")})
public class Indicador implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "idindicador")
    private Integer idindicador;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 500)
    @Column(name = "nombre")
    private String nombre;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 500)
    @Column(name = "formula")
    private String formula;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 500)
    @Column(name = "unidad")
    private String unidad;
    // @Max(value=?)  @Min(value=?)//if you know range of your decimal fields consider using these annotations to enforce field validation
    @Column(name = "base")
    private Float base;
    @Size(max = 500)
    @Column(name = "medicion")
    private String medicion;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "indicadorIdindicador")
    private Collection<Valor> valorCollection;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "indicadorIdindicador")
    private Collection<ObjetivoEstrategico> objetivoEstrategicoCollection;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "indicadorIdindicador")
    private Collection<Semaforo> semaforoCollection;

    public Indicador() {
    }

    public Indicador(Integer idindicador) {
        this.idindicador = idindicador;
    }

    public Indicador(Integer idindicador, String nombre, String formula, String unidad) {
        this.idindicador = idindicador;
        this.nombre = nombre;
        this.formula = formula;
        this.unidad = unidad;
    }

    public Integer getIdindicador() {
        return idindicador;
    }

    public void setIdindicador(Integer idindicador) {
        this.idindicador = idindicador;
        
        this.valorCollection=this.getValorCollection();
        this.semaforoCollection=this.getSemaforoCollection();
    }

    public String getNombre() {
        return nombre;
    }

    public void setNombre(String nombre) {
        this.nombre = nombre;
    }

    public String getFormula() {
        return formula;
    }

    public void setFormula(String formula) {
        this.formula = formula;
    }

    public String getUnidad() {
        return unidad;
    }

    public void setUnidad(String unidad) {
        this.unidad = unidad;
    }

    public Float getBase() {
        return base;
    }

    public void setBase(Float base) {
        this.base = base;
    }

    public String getMedicion() {
        return medicion;
    }

    public void setMedicion(String medicion) {
        this.medicion = medicion;
    }

    @XmlTransient
    public Collection<Valor> getValorCollection() {
        return valorCollection;
    }

    public void setValorCollection(Collection<Valor> valorCollection) {
        this.valorCollection = valorCollection;
    }

    @XmlTransient
    public Collection<ObjetivoEstrategico> getObjetivoEstrategicoCollection() {
        return objetivoEstrategicoCollection;
    }

    public void setObjetivoEstrategicoCollection(Collection<ObjetivoEstrategico> objetivoEstrategicoCollection) {
        this.objetivoEstrategicoCollection = objetivoEstrategicoCollection;
    }

    @XmlTransient
    public Collection<Semaforo> getSemaforoCollection() {
        return semaforoCollection;
    }

    public void setSemaforoCollection(Collection<Semaforo> semaforoCollection) {
        this.semaforoCollection = semaforoCollection;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (idindicador != null ? idindicador.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Indicador)) {
            return false;
        }
        Indicador other = (Indicador) object;
        if ((this.idindicador == null && other.idindicador != null) || (this.idindicador != null && !this.idindicador.equals(other.idindicador))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "negocio.Indicador[ idindicador=" + idindicador + " ]";
    }
    
    public MeterGaugeChartModel getMeterGaugeModel1() {
        
        /*Iterator<Semaforo> it=semaforoCollection.iterator();
        Semaforo s=null;
        List<Number> intervals1 = new ArrayList<Number>();
        while(it.hasNext()){
            s=it.next();
            intervals1.add(s.getLsuperior());
        }*/
           
        List<Number> intervals1 = new ArrayList<Number>();
        intervals1.add(33);
        intervals1.add(66);
        intervals1.add(100);
        
        MeterGaugeChartModel meterGaugeModel1;
        meterGaugeModel1 = new MeterGaugeChartModel(10, intervals1);;
        meterGaugeModel1.setTitle("Semaforo");
        meterGaugeModel1.setSeriesColors("66cc66,E7E658,cc6666");
        meterGaugeModel1.setShowTickLabels(false);
        meterGaugeModel1.setLabelHeightAdjust(110);
        meterGaugeModel1.setIntervalOuterRadius(100);
       
        return meterGaugeModel1;
    }
    
    public LineChartModel createLineModels() {
        LineChartModel lineModel1;
        lineModel1 = initLinearModel();
        lineModel1.setTitle("Historial de Valores - "+this.getNombre());
        lineModel1.setLegendPosition("e");
        Axis yAxis = lineModel1.getAxis(AxisType.Y);
        yAxis.setMin(0);
        yAxis.setMax(10);
        return lineModel1; 
    }
    
    private LineChartModel initLinearModel() {
        LineChartModel model = new LineChartModel();
 
        LineChartSeries series1 = new LineChartSeries();
        series1.setLabel("Series 1");
 
        List<Valor> v=this.get10Valores();
        for(int i=0;i<v.size();i++){
            series1.set(v.get(i).getFecha(), v.get(i).getValor());
        }
       
        model.addSeries(series1);
 
         
        return model;
    }
    
    public List<Valor> get10Valores(){
        List<Valor> valores=new ArrayList();
        Iterator<Valor> vit=this.valorCollection.iterator();
        Valor v=null;
        while(vit.hasNext()){
            v=vit.next();
            valores.add(v);
        }
        
        if(valores.size()>10){
            for(int i=0;i<valores.size()-10;i++){
                valores.remove(i);
            }
        }
        
        return valores;
    }
}
